package com.school.system.controller;

import com.school.system.entity.Student;
import com.school.system.service.StudentService;
import com.school.system.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生控制器
 * 
 * @author 鹿与兔
 * @since 2025-05-22
 */
@RestController
@RequestMapping("/students")
@CrossOrigin
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 分页查询学生列表
     */
    @GetMapping
    public Result<Page<Student>> getStudentPage(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        
        Pageable pageable = PageRequest.of(page, size);
        Page<Student> studentPage = studentService.getStudentPage(pageable);
        return Result.success(studentPage);
    }

    /**
     * 获取所有学生列表
     */
    @GetMapping("/all")
    public Result<List<Student>> getAllStudents() {
        List<Student> students = studentService.getAllStudents();
        return Result.success(students);
    }

    /**
     * 根据ID获取学生信息
     */
    @GetMapping("/{id}")
    public Result<Student> getStudentById(@PathVariable Long id) {
        Student student = studentService.getStudentById(id);
        if (student != null) {
            return Result.success(student);
        } else {
            return Result.error("学生不存在");
        }
    }

    /**
     * 添加学生
     */
    @PostMapping
    public Result<Student> addStudent(@RequestBody Student student) {
        try {
            Student savedStudent = studentService.addStudent(student);
            return Result.success("添加学生成功", savedStudent);
        } catch (Exception e) {
            return Result.error("添加学生失败: " + e.getMessage());
        }
    }

    /**
     * 更新学生信息
     */
    @PutMapping("/{id}")
    public Result<Student> updateStudent(@PathVariable Long id, @RequestBody Student student) {
        try {
            student.setId(id);
            Student updatedStudent = studentService.updateStudent(student);
            return Result.success("更新学生信息成功", updatedStudent);
        } catch (Exception e) {
            return Result.error("更新学生信息失败: " + e.getMessage());
        }
    }

    /**
     * 删除学生
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteStudent(@PathVariable Long id) {
        try {
            studentService.deleteStudent(id);
            return Result.success("删除学生成功");
        } catch (Exception e) {
            return Result.error("删除学生失败: " + e.getMessage());
        }
    }

    /**
     * 根据班级ID获取学生列表
     */
    @GetMapping("/class/{classId}")
    public Result<List<Student>> getStudentsByClassId(@PathVariable Long classId) {
        List<Student> students = studentService.getStudentsByClassId(classId);
        return Result.success(students);
    }

    /**
     * 根据姓名搜索学生
     */
    @GetMapping("/search")
    public Result<List<Student>> searchStudents(@RequestParam String name) {
        List<Student> students = studentService.searchStudentsByName(name);
        return Result.success(students);
    }
}
